package com.buster.dumbshellarts;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.buster.dumbshellarts.SimpleGestureFilter.SimpleGestureListener;

public class DashBoardActivity extends Activity implements
		SimpleGestureListener {

	TextView movieTitle, actors, director, year;
	TextView timerText;
	ToggleButton timerToggle;
	DBHelper myDBHelper = new DBHelper(this);
	Button next;
	String timerSpan;
	// ProgressBar progress;
	private SimpleGestureFilter detector;
	MediaPlayer mp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		detector = new SimpleGestureFilter(this, this);

		/*
		 * progress = (ProgressBar) findViewById(R.id.progressBar);
		 * progress.setVisibility(ProgressBar.VISIBLE);
		 */
		mp = MediaPlayer.create(getApplicationContext(), R.raw.timer);

		try {
			myDBHelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
		try {
			myDBHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}

		movieTitle = (TextView) findViewById(R.id.movieTitleLarge);
		actors = (TextView) findViewById(R.id.actors);
		director = (TextView) findViewById(R.id.director);
		year = (TextView) findViewById(R.id.year);
		timerText = (TextView) findViewById(R.id.timer);

		populateMovie();
		next = (Button) findViewById(R.id.nextButton);
		next.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				populateMovie();
				// progress.setVisibility(ProgressBar.GONE);
			}
		});

		timerToggle = (ToggleButton) findViewById(R.id.timerToggle);
		timerToggle.setOnClickListener(new View.OnClickListener() {
			private boolean running = false;
			private CountDownTimer countTimer;

			@Override
			public void onClick(View v) {
				if (!running) {
					running = true;
					int time = Integer.parseInt(getTimeSpan() + "000");
					countTimer = new CountDownTimer(time, 1000) {
						@Override
						public void onFinish() {
							timerText.setText("Done!");
							timerToggle.setChecked(false);
							timerToggle.setText("Start");
							running = false;
						}

						@Override
						public void onTick(long sec) {
							timerText.setText("Left: " + sec / 1000);
							if (sec / 1000 <= 10) {
								mp.start();
							}
						}
					}.start();
				} else {
					countTimer.cancel();
					timerText.setText("00");
					running = false;
				}
			}
		});
		// progress.setVisibility(ProgressBar.GONE);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.settings_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.settingsItem:
			startActivity(new Intent(DashBoardActivity.this,
					PrefsActivity.class));
			break;

		case R.id.exitItem:
			mp.stop();
			this.finish();
			break;
		}
		return true;
	}

	private String getSelectedLanguage() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(DashBoardActivity.this);

		String language = prefs.getString("language", "0");
		return language;
	}

	private String getTimeSpan() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(DashBoardActivity.this);

		String time = prefs.getString("timeSpan", "30");
		return time;
	}

	private void SetMovieTitle(Cursor cursor) {
		if (cursor != null) {
			String title = cursor.getString(cursor
					.getColumnIndex(DBHelper.COL_TITLE));
			String URL = cursor.getString(cursor
					.getColumnIndex(DBHelper.COL_URL));

			movieTitle.setText(Html.fromHtml("<a href=" + URL + ">" + title
					+ "</a> "));
			movieTitle.setMovementMethod(LinkMovementMethod.getInstance());

			actors.setText(cursor.getString(cursor
					.getColumnIndex(DBHelper.COL_ACTORS)));
			director.setText(cursor.getString(cursor
					.getColumnIndex(DBHelper.COL_DIRECTOR)));
			year.setText(cursor.getString(cursor
					.getColumnIndex(DBHelper.COL_YEAR)));
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent me) {
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}

	@Override
	public void onSwipe(int direction) {
		// String str = "";

		populateMovie();
		switch (direction) {

		case SimpleGestureFilter.SWIPE_RIGHT:
			// str = "Swipe Right";
			break;
		case SimpleGestureFilter.SWIPE_LEFT:
			// str = "Swipe Left";
			break;
		case SimpleGestureFilter.SWIPE_DOWN:
			// str = "Swipe Down";
			break;
		case SimpleGestureFilter.SWIPE_UP:
			// str = "Swipe Up";
			break;

		}
		// Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onDoubleTap() {
		// Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
		populateMovie();
	}

	private void populateMovie() {
		if (getSelectedLanguage().equals("0")) {
			Cursor cursor1 = myDBHelper.getRandomMovie();
			SetMovieTitle(cursor1);
			cursor1.close();
		} else {
			Cursor cursor = myDBHelper.getRandomMovieByLanguage(Integer
					.parseInt(getSelectedLanguage()));
			SetMovieTitle(cursor);
			cursor.close();
		}
	}
}
